package cn.samir.sudoku.core;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Align;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

public class ScaledSudokuView extends ImageView {

	String content = "700120300500004007030000610000200490000050000012007000023000050900800001007065004";
	Paint paint;
	int mwidth;
	int mHeight;
	boolean isContainsId = true;
	public ScaledSudokuView(Context context, AttributeSet attrs,
			int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		inits(context);
	}

	public boolean isContainsId() {
		return isContainsId;
	}

	public void setContainsId(boolean isContainsId) {
		this.isContainsId = isContainsId;
	}

	private void inits(Context context) {
		paint = new Paint();
		paint.setColor(Color.BLACK);

		paint.setAntiAlias(true);
		paint.setTextAlign(Align.CENTER);
		paint.setStrokeWidth(1.0f);

	}

	public ScaledSudokuView(Context context, AttributeSet attrs) {
		super(context, attrs);
		inits(context);
	}

	public ScaledSudokuView(Context context) {
		super(context);
		inits(context);
	}

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
		invalidate();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		int size = 0;
		mwidth = getWidth();
		mHeight = getHeight();
		if (content != null) {
			size = content.length();
		}
		// canvas.drawText("12345", 0, 0, paint);
		// canvas.drawColor(Color.BLUE);
		//canvas.drawLine(0, 0, mwidth, 0, paint);
		//canvas.drawLine(mwidth, 0, mwidth, mHeight, paint);
		//canvas.drawLine(0, mHeight, mwidth, mHeight, paint);
		int splitw = mwidth / 9;
		int splith = mHeight / 9;
		// for (int i = 0; i < 9; i++) {
		// canvas.drawLine((i % 9) * splitw + 2, 0, (i % 9) * splitw
		// + 2, mHeight, paint);
		// canvas.drawLine(0, (i % 9) * splith + 2, mwidth, (i % 9)
		// * splith + 2, paint);
		// }

		paint.setTextSize(splitw);
		for (int i = 0; i < size; i++) {
			// canvas.drawText(bytes[i], i / 9, y, paint);
			if (i < 8 && i > 0 && isContainsId) {
				paint.setColor(0xFF00BFFF);
				canvas.drawText(content.charAt(i) + "", ((i % 9) * splitw)
						+ splith / 2, (i / 9) * splith + splith, paint);
			}else if (content.charAt(i) != '0') {
				paint.setColor(Color.BLACK);
				canvas.drawText(content.charAt(i) + "", ((i % 9) * splitw)
						+ splith / 2, (i / 9) * splith + splith, paint);
			}

		}

	}

}
